package com.candy.candysocket.netty;

import com.gitee.candy.dateTime.DateUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

import java.util.Date;

/**
 * @author suYanLong
 * @version 1.0
 * @date 2024/7/19 8:49
 * @Description 描述
 */
public class HeartBeatClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println(DateUtil.formatDate(new Date(), "yyyy-MM-dd hh:mm:ss") + "客户端循环心跳监测发送");
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.WRITER_IDLE) {
//                if (curTime < beatTime) {
//                    curTime++;
                ctx.writeAndFlush("heartBeat");
//                }
            }
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // 连接断开时被调用
        System.out.println("Connection lost, Client attempting reconnection...");
//        reconnect(ctx);
    }

}
